Given the following base C++ class, class area_cl { public: double height; double width; }; Create two derived classes called rectangle and isosceles that inherit area_cl. Have each class include a...

      

Given the following base C++ class,
class area_cl {
public:
double height;
double width;
};
Create two derived classes called rectangle and isosceles that inherit area_cl. Have each class include a function called area()
that returns the area of a rectangle or isosceles triangle, as appropriate. Use parameterized constructors to initialize height and width.

  

Answers


Davis
#include
using namespace std;
class area_cl {
public:
double height;
double width;
};
class rectangle : public area_cl {
public :
rectangle (double h, double w) ;
double area();
};
class isosceles : public area {
public:
isosceles (double h, double w);
double area();
};
rectangle ::rectangle(double h, double w)
{
height = h;
width = w;
}
isosceles ::isosceles (double h, double w)
{
height = h;
width = w;
}
double rectangle :: area()
{
return width *height;
}
double isosceles :: area()
{
return 0.5 * width * height;
}
int main()
{
rectangle b(10.0, 5.0);
isosceles i(4.0, 6.0);
cout << "Rectangle: " << b.area() << "\n";
cout << "Triangle: " << i.area() << "\n";
return 0;
}
Githiari answered the question on May 12, 2018 at 16:23


Next: “Commercial banks are not only dealers of money but manufacturers of money and credit also.” Explain the statement.
Previous: Use a C++ union class to swap the low- and high-order bytes of an integer (assuming 16-bit integer; if the computer uses 32-bit integers, swap...

View More Computer Science Questions and Answers | Return to Questions Index


Exams With Marking Schemes

Related Questions